package com.cuit.xsc.weishare.activity;

import java.util.List;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.adapter.AccountManagerAdpter;
import com.cuit.xsc.weishare.model.UserInfo;
import com.cuit.xsc.weishare.services.UserInfoServices;
import com.cuit.xsc.weishare.util.ConfigUtil;
import com.cuit.xsc.weishare.util.IMessageDialogListener;
import com.cuit.xsc.weishare.util.MessageDialog;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class AccountManagerAct extends BaseActivity implements IMessageDialogListener
{
	private ListView listView = null;
	private String currentType = null;
	private List<UserInfo> list = null;
	private UserInfoServices uServices = null;
	private AccountManagerAdpter adpter = null;
	private UserInfo userInfo = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accountmanager);
		listView = (ListView) findViewById(R.id.lvAccountManager);
		
		currentType = getIntent().getStringExtra(ConfigUtil.TYPE);
		uServices = new UserInfoServices(this);
		list = findUserInfos(currentType);
		
		adpter = new AccountManagerAdpter(this);
		adpter.setListView(listView);
		if (list != null)
		{
			adpter.setList(list);
			listView.setAdapter(adpter);
		}
		listView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				deleteUser(arg2);
			}
		});
		
		
	}
	
	private List<UserInfo> findUserInfos(String userType)
	{
		return uServices.findUsersByType(userType);
	}
	
	private void deleteUser(int postion)
	{
		MessageDialog dialog = new MessageDialog(this);
		userInfo = list.get(postion);
		if (userInfo != null)
		{
			String msg = "你确定要删除"+userInfo.getUserName()+"吗?";
			dialog.ShowConfirm(12, "删除账户", msg, this);
		}
		
	}

	@Override
	public void onDialogClickOk(int requestCode)
	{
		switch (requestCode)
		{
		case 12:
			//finish();
			//System.out.println(userInfo.getUserId()+"_"+userInfo.getId()+"_"+userInfo.getUserName());
			boolean flag = uServices.deleteUserInfo(userInfo);
			MessageDialog dialog = new MessageDialog(this);
			if (flag)
			{
				dialog.ShowToast("删除成功！");
			}
			else
			{
				dialog.ShowToast("删除失败！");
			}
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onDialogClickCancel(int requestCode)
	{
		
	}

	@Override
	public void onDialogClickClose(int requestCode)
	{
		
	}

	
}
